import 'dart:io';

import 'package:android_intent_plus/android_intent.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:wuxu_flutter_lianxi/page/main/category/category_page.dart';
import 'package:wuxu_flutter_lianxi/page/main/mine/mine_page.dart';

import '../../localization/default_localizations.dart';
import '../../style/wx_style.dart';
import '../../widget/home_tabbar_widget.dart';
import 'home/home_page.dart';
import 'main_drawer.dart';

///
/// 主页
///
/// 首页、分类、我的
///
class MainPage extends StatefulWidget{
  static String path="main";
  @override
  State<StatefulWidget> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> with TickerProviderStateMixin{
  //tab 设定GlobalKey 保证全局唯一，且可获取其State
  final GlobalKey<HomePageState> homeKey = GlobalKey();
  final GlobalKey<CategoryState> categoryKey = GlobalKey();
  final GlobalKey<MineState> mineKey = GlobalKey();


  _renderTab(icon,text){
    return Tab(
      child:Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Icon(icon,size: 16.0,),
          Text(text)
        ],
      )
    );
  }


  @override
  void initState(){
    super.initState();
  }


  @override
  Widget build(BuildContext context) {
    List<Widget> tabs=[
      //TODO:使用自定义图标
      _renderTab(Icons.ac_unit_sharp, WxLocalizations.languageResString(context).mainTabHome),
      _renderTab(Icons.ac_unit_sharp, WxLocalizations.languageResString(context).mainTabCategories),
      _renderTab(Icons.ac_unit_sharp, WxLocalizations.languageResString(context).mainTabMine),
    ];

    //退出
    dialogExitApp(BuildContext context) async{
      //如果是 android 回到桌面
      if (Platform.isAndroid) {
        AndroidIntent intent = const AndroidIntent(
          action: 'android.intent.action.MAIN',
          category: "android.intent.category.HOME",
        );
        await intent.launch();
      }
    }


    //PopScope 用于处理系统手势返回
    return PopScope(
        canPop: false,
        onPopInvoked: (didPop){
          dialogExitApp(context);
        },
        child: CustomScaffoldWidget(
          drawer:const MainDrawer(),
          position: TabBarPosition.bottom,
          tabItems: tabs,
          tabViews: [
            HomePage(key:homeKey),
            CategoryPage(key:categoryKey),
            MinePage(key:mineKey),
          ],
          onDoublePress: (index){
            switch (index) {
            }
          },
          backgroundColor: WxColors.primarySwatch,
          indicatorColor: Colors.transparent,
          title:const Text("小兔鲜儿"),
        )

    );
  }

}